<?php


namespace App\Http\Controllers\Admin;


use App\DTOS\Admin\HorseRaceLampDTO;
use App\Http\Controllers\Controller;
use App\Models\Game\HorseRaceLamp;
use App\Models\Game\RoleCoordinates;
use App\Services\Game\HorseRaceLampService;
use App\Services\Game\RoleCoordinatesService;
use App\Utils\ResponseUtil;
use Illuminate\Support\Facades\DB;

class RoleCoordinatesController extends Controller
{
    private RoleCoordinatesService $RoleCoordinatesService;

    /**
     * HorseRaceLampController constructor.
     * @param RoleCoordinatesService $RoleCoordinatesService
     */
    public function __construct(RoleCoordinatesService $RoleCoordinatesService)
    {
        $this->RoleCoordinatesService = $RoleCoordinatesService;
    }

    public function add()
    {
        $input = request()->all();
        $data = [];
        $data['RoleId'] = data_get($input,'RoleId');
        $data['content'] = data_get($input,'content');
        if($data['content'] ){
            $contentParts = explode('*', $data['content']);
            if (count($contentParts) === 3) {
                $data['MapID'] = $contentParts[0];
                $data['PosX'] = $contentParts[1];
                $data['PosY'] = $contentParts[2];
            }
        }
        $data['type'] = 0;
        $data['updated_at'] = $data['created_at'] = date("Y-m-d H:i:s");
        RoleCoordinates::query()->insert($data);
        return ResponseUtil::success();
    }

    public function paginate()
    {
        $dto = new RoleCoordinates(request()->all());
        $paginate = $this->RoleCoordinatesService->paginate($dto);
        $data = $paginate->items();
        $res = [];
        $res['code'] = 0;
        $res['data'] = $data;
        return ResponseUtil::json($res);
    }
    public function confirm($id){
        $row = RoleCoordinates::query()->find($id);
        $row->confirm = 1;
        $row->save();
        return ResponseUtil::success();
    }
    public function delete($id)
    {
        $detail = RoleCoordinates::query()->find($id);
        $detail->delete();
        return ResponseUtil::success();
    }
    public function detail($id)
    {
        $detail = RoleCoordinates::query()->find($id);
        return ResponseUtil::success($detail);
    }
    public function edit($id)
    {
        DB::beginTransaction();
        try {
            $input = request()->all();

            $data = [];
            $data['RoleId'] = data_get($input,'RoleId');
            $data['content'] = data_get($input,'content');
            if($data['content'] ){
                $contentParts = explode('*', $data['content']);
                if (count($contentParts) === 3) {
                    $data['MapID'] = $contentParts[0];
                    $data['PosX'] = $contentParts[1];
                    $data['PosY'] = $contentParts[2];
                }
            }
            $data['type'] = 0;


            $data['updated_at'] = date("Y-m-d H:i:s");
            RoleCoordinates::query()->where('id',$id)->update($data);
            DB::commit();
            return ResponseUtil::success();
        }catch(\Exception $e){
            DB::rollBack();
            return ResponseUtil::error($e->getMessage());
        }
    }

}
